package com.ljy.utyun.swagger;

import com.ljy.utyun.sql.SqlFieldCollector;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lijunying
 * @date 2020/10/15 17:30
 */
public class PojoSwaggerProcessor extends SwaggerProcessor {

    Map<String,Set<String>> fieldHashMap;

    public PojoSwaggerProcessor(Map<String,Set<String>> fieldHashMap){
        this.fieldHashMap=fieldHashMap;
    }

    @Override
    public boolean checkCanProcess(File file) {
        String path=file.getAbsolutePath();
//        System.out.println("path:"+path);
        return path.indexOf("pojo")!=-1||file.getName().toLowerCase().lastIndexOf("vo.java")!=-1
                ||file.getName().toLowerCase().lastIndexOf("bo.java")!=-1;
    }

    @Override
    public List<String> doProcess(File file,List<String> valueList){
//        加入import
        valueList.add(2,"import io.swagger.annotations.ApiModelProperty;");

        //        加入注解
        for(int i=0;i<valueList.size();i++){
            String line=valueList.get(i);
            int tabIndex=-1;
            if((tabIndex=checkIsFieldLine(line))==-1){
                continue;
            }
            String name=getChname(valueList,i);

            StringBuilder tabSb=new StringBuilder("\t");
//            for(int index=0;index<tabIndex;index++){
//                tabSb.append("\t");
//            }

            valueList.add(getAddLineIndex(valueList,i),tabSb.toString()+"@ApiModelProperty(\""+name+"\")");
            i++;
        }
        return valueList;
    }

    private int checkIsFieldLine(String line) {
//        已经注释了
        if(line.trim().startsWith("//")){
            return -1;
        }
        return line.indexOf("private");
    }

    private String getChname(List<String> valueList,int index){
        String key=getField(valueList,index);
        index--;

        boolean flag=true;
        String[] identifier={"private","protected","public"};
        out:while(valueList.get(index).indexOf("/**")==-1){
            //到了上一个参数
            for(String s:identifier){
                if(valueList.get(index).indexOf(s)!=-1){
                    flag=false;
                    break out;
                }
            }
            index--;
        }
        if(flag){
            index++;
            String line=valueList.get(index);
            int start=line.indexOf("* ");
            if(start==-1){
                return key;
            }
            return line.substring(start+2);
        }else{

            if(fieldHashMap.containsKey(key)){
                Set<String> valueSet=fieldHashMap.get(key);
                StringBuilder sb=new StringBuilder();
                valueSet.stream().forEach(v->{
                    sb.append(v+";");
                });
                return sb.toString().substring(0,sb.toString().length()-1);
            }else{
                return key;
            }
        }

    }

    //获取字段名
    private String getField(List<String> valueList,int originIndex){
        String line=valueList.get(originIndex).trim();
        int endIndex;
        if((endIndex=line.indexOf("="))!=-1){
            line=line.substring(0,endIndex).trim();
        }
        String[] lineArray=line.split(" ");
        String field=lineArray[lineArray.length-1];
        char[] chars=field.toCharArray();
        int i=0;
        for(;i<chars.length;i++){
            char c=chars[i];
            if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
                continue;
            }else{
                break;
            }
        }
        String key=field.substring(0,i);
        return key;
    }

}
